package com.kingmed.kmss.admin.modules.sys.service.helper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.kingmed.kmss.admin.modules.sys.model.SysOpenGroup;
import com.kingmed.kmss.admin.modules.sys.model.SysOpenRelation;
import com.kingmed.kmss.admin.modules.sys.model.SysOpenUrl;
import com.kingmed.kmss.common.constant.sys.CacheConstants;
import com.kingmed.kmss.common.constant.common.CommonConstants;
import com.kingmed.kmss.common.constant.common.OperatorConstants;
import com.kingmed.kmss.common.service.sys.RedisService;
import com.kingmed.kmss.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外开放接口关系缓存操作类
 */
@Slf4j
@Component
@AllArgsConstructor
public class OpenRelationCacheHelper {

    private final RedisService redisService;

    /**
     * 缓存key
     */
    private static final String BASE_KEY = CacheConstants.OPEN_URL_KEY;

    /**
     * 获取已有的缓存
     *
     * @return
     */
    public Map<String, HashMap<String, String>> getMap() {
        // 尝试获取已有的缓存
        try {
            Map<String, HashMap<String, String>> bigMap = (Map<String, HashMap<String, String>>) redisService.get(BASE_KEY);
            return bigMap;
        } catch (Exception e) {
            log.error(" === getMap error, e={} ===", e);
        }
        return MapUtil.newHashMap();
    }

    /**
     * 将数据放进缓存
     *
     * @return
     */
    public boolean putMap(Map<String, HashMap<String, String>> cacheMap) {
        try {
            if (CollUtil.isNotEmpty(cacheMap)) {
                redisService.hSetAll(BASE_KEY, cacheMap);
                return true;
            }
        } catch (Exception e) {
            log.error(" === putMap error, e={} ===", e);
        }
        return false;
    }

    /**
     * 删除缓存
     *
     * @return
     */
    public boolean remove() {
        try {
            redisService.del(BASE_KEY);
            return true;
        } catch (Exception e) {
            log.error(" === putMap error, e={} ===", e);
        }
        return false;
    }

    /**
     * 删除缓存
     *
     * @return
     */
    public boolean remove(String groupTokenKey) {
        try {
            if (StringUtils.isNotBlank(groupTokenKey)) {
                redisService.hDel(BASE_KEY, groupTokenKey);
                return true;
            }
        } catch (Exception e) {
            log.error(" === putMap error, e={} ===", e);
        }
        return false;
    }

    /**
     * 组装缓存对象
     *
     * @param openUrlMap
     * @param openGroupMap
     * @param listMap
     * @return
     */
    public Map<String, HashMap<String, String>> initBigMap(Map<Long, SysOpenUrl> openUrlMap, Map<Long, SysOpenGroup> openGroupMap, Map<Long, List<SysOpenRelation>> listMap) {

        Map<String, HashMap<String, String>> bigMap = new HashMap<>(listMap.size());

        listMap.forEach((groupIdKey, values) -> {
            SysOpenGroup openGroup = openGroupMap.get(groupIdKey);
            // 只加载正常启用的数据
            if (null != openGroup && CommonConstants.EnableStatusStr.ENABLE.equals(openGroup.getStatus())) {
                HashMap<String, String> cacheMap = new HashMap<>();
                StringBuilder sb = new StringBuilder();
                for (SysOpenRelation urls : values) {
                    SysOpenUrl url = openUrlMap.get(urls.getUrlId());
                    if (null != url && CommonConstants.EnableStatusStr.ENABLE.equals(url.getStatus())) {
                        sb.append(url.getPath()).append(OperatorConstants.DH_KEY);
                    }
                }
                cacheMap.put(openGroup.getAppId(), sb.toString());
                bigMap.put(openGroup.getAppKey(), cacheMap);
            }
        });

        return bigMap;
    }
}
